package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.AttendanceSubBean;
import com.lzx.iteam.util.DateUtil;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 12月 07日
 * Time: 下午12:18
 *
 * @QQ : 1234567890
 * **********************************
 */
public class AttendanceSubSignAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<AttendanceSubBean> mData = new ArrayList<>();
    private SubSignListener mListener;

    public AttendanceSubSignAdapter(Context context) {
        mContext = context;
    }

    public void bindData( ArrayList<AttendanceSubBean> mData){
        this.mData = mData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData == null ? 0:mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final AttendanceSubBean subBean = mData.get(position);
        AttendanceSubBean lastSubBean = null;
        if (position != 0){
            lastSubBean = mData.get(position -1);
        }
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.attendance_main_item_layout,null);
            holder.tvDate = (TextView) convertView.findViewById(R.id.tv_at_sub_item);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_at_sub_item_name);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_at_sub_item_time);
            holder.tvSign = (TextView) convertView.findViewById(R.id.tv_at_sub_item_sign);
            holder.tvSignTime = (TextView) convertView.findViewById(R.id.tv_at_sub_item_sign_time);
            holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_at_sub_item);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        if (subBean.getSignType() == -1){
            holder.ivIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.legwork));
            holder.tvDate.setVisibility(View.GONE);
            holder.tvSign.setVisibility(View.VISIBLE);
            holder.tvSignTime.setVisibility(View.GONE);
            holder.tvSign.setText("进入");
            if (subBean.isEnable()){
                holder.tvSign.setEnabled(true);
            }else{
                holder.tvSign.setEnabled(false);
            }
            holder.tvTime.setVisibility(View.GONE);
            holder.tvTitle.setText("外勤");

        }else{
            holder.tvSign.setText("打卡");
            holder.tvTime.setVisibility(View.VISIBLE);
            if (lastSubBean != null && subBean.getDate() == lastSubBean.getDate()){
                holder.tvDate.setVisibility(View.GONE);
            }else{
                holder.tvDate.setVisibility(View.VISIBLE);
                holder.tvDate.setText(DateUtil.getSubDateTitle(subBean.getDate() * 1000)  + subBean.getSub_name());
            }
            if (subBean.getSignType() == 1 || subBean.getSignType() == 4){
                holder.tvTitle.setText("上班");
                if (subBean.isEnable()){
                    holder.ivIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.sign_in));
                }else{
                    holder.ivIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.sign_in_ok));
                }
            }else{
                holder.tvTitle.setText("下班");
                if (subBean.isEnable()){
                    holder.ivIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.sign_out));
                }else{
                    holder.ivIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.sign_out_ok));
                }
            }
            String time = subBean.getTimeString();
            holder.tvTime.setText(time.replace("-","次日"));
//            holder.tvTime.setText(DateUtil.getHourAndMin(subBean.getTime() * 1000));
            if (subBean.isSign()){
                holder.tvSignTime.setVisibility(View.VISIBLE);
                holder.tvSign.setVisibility(View.GONE);
                if (subBean.getTimeString().contains("-") && subBean.getSignTime() > (subBean.getDate() + 24 * 60 * 60)){
                    holder.tvSignTime.setText("已打卡次日" + DateUtil.getHourAndMin(subBean.getSignTime() * 1000));
                }else{
                    holder.tvSignTime.setText("已打卡" + DateUtil.getHourAndMin(subBean.getSignTime() * 1000));
                }
                if (subBean.getSignType() == 1 || subBean.getSignType() == 4){
                    if (subBean.isLate()){
                        holder.tvSignTime.setTextColor(mContext.getResources().getColor(R.color.red));
                    }else{
                        holder.tvSignTime.setTextColor(mContext.getResources().getColor(R.color.schedule_notbusy_text));
                    }
                }else {
                    if (subBean.isLate()){
                        holder.tvSignTime.setTextColor(mContext.getResources().getColor(R.color.schedule_notbusy_text));
                    }else{
                        holder.tvSignTime.setTextColor(mContext.getResources().getColor(R.color.red));
                    }
                }

            }else{
                holder.tvSignTime.setVisibility(View.GONE);
                holder.tvSign.setVisibility(View.VISIBLE);
                if (subBean.isEnable()){
                    holder.tvSign.setEnabled(true);
                    holder.tvSign.setText("打卡");
                }else{
                    holder.tvSign.setEnabled(false);
                    if (subBean.getEnableState() == -1){
                        holder.tvSign.setText("已过期");
                    }else if(subBean.getEnableState() == 1){
                        holder.tvSign.setText("未启用");
                    }else{
                        holder.tvSign.setText("打卡");
                    }

                }

            }
        }
        holder.tvSign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.signAction(subBean,position);
            }
        });
        return convertView;
    }

    class ViewHolder{
        TextView tvDate;
        TextView tvTitle,tvTime,tvSignTime,tvSign;
        ImageView ivIcon;

    }

    public interface SubSignListener{
        void signAction(AttendanceSubBean data,int position);
    }

    public void setSignListener(SubSignListener listener){
        this.mListener = listener;
    }


}
